package slavik.voloshyn.vashmagazyn;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class DownloadFile extends Thread
{
	private static final int CONTAINERS_COUNT = 6;

	private final Object monitor;
	private final String[] countPages = new String[CONTAINERS_COUNT];

	protected DownloadFile(Object theMonitor)
	{
		monitor = theMonitor;

	}

	@Override
	public void run()
	{
		super.run();
		deleteFile();
		findCountPages();
		downloadPages();
		synchronized (monitor)
		{
			monitor.notify();
		}
	}

	private void findCountPages()
	{
		System.out.println("Start findCountPages");
		for (int i = 0; i < countPages.length; i++)
		{
			HttpURLConnection connect = null;
			InputStream in = null;

			try
			{

				String url = Const.CONTAINERS + (i + 1) + Const.PAGES + "1";
				connect = (HttpURLConnection) new URL(url).openConnection();
				in = connect.getInputStream();
				connect.connect();
				StringBuffer stringBuffer = new StringBuffer();
				byte[] buffer = new byte[1024];
				int sz = 0;
				while ((sz = in.read(buffer)) > 0)
				{
					stringBuffer.append(new String(buffer));
				}
				in.close();
				int lastIndex = stringBuffer
				        .lastIndexOf(Const.TAG_START_PAGE_COUNT);
				if (lastIndex >= 0)
				{
					countPages[i] = stringBuffer.substring(lastIndex
					        + Const.TAG_START_PAGE_COUNT.length(), stringBuffer
					        .indexOf(Const.TAG_END_PAGE_COUNT, lastIndex));
				}
				else
				{
					countPages[i] = "1";
				}
				stringBuffer.delete(0, stringBuffer.length());
				System.out.println("Max pages:" + countPages[i]);

			}
			catch (IOException e)
			{
				System.out.println("IOException:" + e.toString());
				e.printStackTrace();
			}
			finally
			{
				if (connect != null)
				{
					connect.disconnect();
				}
			}

		}
		System.out.println("Finish search pages");
	}

	private void downloadPages()
	{
		HttpURLConnection connect = null;
		InputStream in = null;
		FileOutputStream f = null;
		try
		{

			System.out.println("Start download");
			for (int i = 0; i < countPages.length; i++)
			{
				for (int j = 1; j <= Integer.parseInt(countPages[i]); j++)
				{
					String url = Const.CONTAINERS + (i + 1) + Const.PAGES + j;
					connect = (HttpURLConnection) new URL(url).openConnection();
					in = connect.getInputStream();
					connect.connect();
					f = new FileOutputStream(Const.PATH, true);
					byte[] buffer = new byte[1024];
					int sz = 0;
					while ((sz = in.read(buffer)) > 0)
					{
						f.write(buffer, 0, sz);
					}
					in.close();
					f.close();
					System.out.println("Downloaded page:" + url);
				}
				System.out.println((i + 1) + ") CONTAINERS downloaded");
			}
		}
		catch (IOException e)
		{
			System.out.println("IOException:" + e.toString());
			e.printStackTrace();
		}
		finally
		{
			if (connect != null)
			{
				connect.disconnect();
			}
		}

	}

	private void deleteFile()
	{
		File file = new File(Const.PATH);
		if (file.exists())
		{
			file.delete();
		}
	}
}
